package cn.edu.scut.sys.controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.scut.sys.service.TrainItemService;
import cn.edu.scut.sys.service.TrainScoreItemService;
import cn.edu.scut.sys.service.TrainScoreService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.utils.StringUtils;
import cn.edu.scut.sys.vo.TrainItem;
import cn.edu.scut.sys.vo.TrainScore;
import cn.edu.scut.sys.vo.TrainScoreItem;


@Controller
@RequestMapping("/trainScoreItem")
public class TrainScoreItemController {

	private TrainScoreItemService trainScoreItemService;
	private TrainItemService trainItemService;
	private TrainScoreService trainScoreService;
	
	
	@Resource(name="trainScoreItemService")
	public void setTrainScoreItemService(TrainScoreItemService trainScoreItemService) {
		this.trainScoreItemService = trainScoreItemService;
	}
	
	@Resource(name="trainItemService")
	public void setTrainItemService(TrainItemService trainItemService) {
		this.trainItemService = trainItemService;
	}
	
	@Resource(name="trainScoreService")
	public void setTrainScoreService(TrainScoreService trainScoreService) {
		this.trainScoreService = trainScoreService;
	}
	/**
	 * 
	 * @param page
	 * @param rows
	 * @param sidx
	 * @param sord
	 * @param search
	 * @param searchField
	 * @param searchOper
	 * @param searchString
	 * @param filters
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public DataResponse<TrainScoreItem> list(@RequestParam(defaultValue="1",value="page") String page,  
            @RequestParam(defaultValue="20",value="rows") String rows,  
            @RequestParam("sidx") String sidx,  
            @RequestParam("sord") String sord,  
            @RequestParam("search") boolean search,  
            @RequestParam(required=false,value="searchField") String searchField,  
            @RequestParam(required=false,value="searchOper") String searchOper,  
            @RequestParam(required=false,value="searchString") String searchString,  
            @RequestParam(required=false,value="filters") String filters  
            ,HttpServletRequest req){  
		 try {  
	            DataRequest request = new DataRequest();  
	            request.setPage(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));  
	            request.setRows(StringUtils.isEmpty(rows) ? 20 : Integer.valueOf(rows));  
	            request.setSidx(sidx);  
	            request.setSord(sord);  
	            request.setSearch(search);  
	            request.setSearchField(searchField);  
	            request.setSearchOper(searchOper);  
	            request.setSearchString(searchString);  
	            String trainScoreId=req.getParameter("trainScoreId");
	      
	            return trainScoreItemService.list(request,trainScoreId);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	        return null;  
	}
	
	@RequestMapping("/getTrainItem/{trainScoreId}")
	public String getTrainItem(@PathVariable(value = "trainScoreId") String trainScoreId,HttpServletRequest request){

		request.setAttribute("trainItemList", trainItemService.getAll());
		if (trainScoreId != null)
			request.setAttribute("trainScoreId", trainScoreId);
		else
			return "/page/trainScoreItemList";
		
		
		return "/page/trainScoreItemAdd";
	}
	
	@RequestMapping("/add")
	public String add(String trainItemId, String score,HttpServletRequest request){
		String trainScoreId="0";
		try{
			trainScoreId=request.getParameter("trainScoreId").toString();
			TrainItem trainItem=new TrainItem();
			TrainScore trainScore=new TrainScore();
			TrainScoreItem trainScoreItem = new TrainScoreItem();
			// 增加项目
			if (trainItemId!=null)
				trainItem=trainItemService.getTrainItem(Long.parseLong(trainItemId));
			
			
			if (trainScoreId != null)
				trainScore=trainScoreService.get(Long.parseLong(trainScoreId));
			else
				return "/page/trainScoreItemList";
			
			trainScoreItem.setTrainItem(trainItem);
			trainScoreItem.setTrainScore(trainScore);
			trainScoreItem.setScore(Integer.parseInt(score));
			trainScoreItemService.add(trainScoreItem);
			request.setAttribute("status", 1);
			
		}catch(Exception ex){
		
			request.setAttribute("status", 0);
		}
		request.setAttribute("trainScoreId", trainScoreId);
		return "/page/trainScoreItemAdd";
	}
	
	
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable(value = "id") Long id,HttpServletRequest request){
		if (id==null) {
			return "redirect:/page/trainScoreItemList.jsp";
		}
		
		TrainScoreItem trainScoreItem=trainScoreItemService.get(id);
		
		if (null == trainScoreItem) {
			return "redirect:/page/trainScoreItemList.jsp";
		}
		
		request.setAttribute ("trainScoreItem", trainScoreItem);
		request.setAttribute("trainItemList", trainItemService.getAll());
		return "page/trainScoreItemEdit";
	}
	
	@RequestMapping("/update")
	public String update(String score,String id,String trainItemId,String trainScoreId, HttpServletRequest request){
		TrainScoreItem trainScoreItem = new TrainScoreItem();
		try{
		request.setAttribute("trainScoreId", trainScoreId);
		trainScoreItem.setTrainItem(trainItemService.getTrainItem(Long.parseLong(trainItemId)));
		trainScoreItem.setScore(Integer.parseInt(score));
		trainScoreItem.setId(Long.parseLong(id));
		trainScoreItem.setTrainScore(trainScoreService.get(Long.parseLong(trainScoreId)));
		trainScoreItemService.update(trainScoreItem);
		request.setAttribute("status", 1);
		
		return "/page/trainScoreItemEdit";
		}
		catch(Exception e){
			request.setAttribute("status", 0);
			request.setAttribute("trainScoreId", trainScoreId);
			return "/page/trainScoreItemEdit";
		}
	}
	
	
	@RequestMapping("/del/{id}")
	public String del(@PathVariable(value = "id") int id,HttpServletRequest request){
	//	trainScoreItemService.delTrainScoreItem(id);
	return "redirect:/page/trainScoreItemList.jsp";
	}
}
